<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib uri="http://agi.corp.org/authz" prefix="authz"%>
<%@ page import="th.co.mfec.mca.common.PrivConst"%>
<%@ page import="th.co.mfec.mca.common.ProjectConstant"%>

<script type="text/javascript" src="${requestScope.jsBase}/jQuery.dualListBox-1.0.1.min.js"></script>

<script language="javascript" type="text/javascript">
        $j(function() {
            $j.configureBoxes();
        });
    </script>

<!-- body content -->
<!-- / body content -->
<div id="content">
<s:form id="mainForm" action="userProfileManagement-add.htm" method="post" >

 <s:hidden id="editMode" name="editMode" />
 
 <s:if test="%{editMode == 'true'}">
 	<s:hidden name="userProfileBean.uspId" />
 	<s:hidden name="userProfileBean.uspPassword" />
 	<input type="hidden" id="correctOldPassword" value="<s:property value="userProfileBean.uspPassword" />" />
 </s:if>
 
 <div class="tableContent">
 	<div id="dialoginfo"></div>
    <div class="topic">Add/Edit User Profile</div>
    
    <div class="tableContent">
            <table>
            <colgroup class="L"></colgroup><colgroup class="R"></colgroup><colgroup class="L"></colgroup><colgroup class="R"></colgroup>
              <tr>
                 <s:if test="%{editMode == 'true'}">
                 	<th nowrap="nowrap"><s:text name="label.username" /> : </th>
                	<td>
                 		<s:textfield id="uspUserName" name="userProfileBean.uspUserName" maxlength="50" readonly="true"/>
                 </s:if>
                 <s:else>
                 	<th nowrap="nowrap"><s:text name="label.username" /><span class="star">*</span> : </th>
                	<td>
                 	<s:textfield id="uspUserName" name="userProfileBean.uspUserName" maxlength="20" />
                 </s:else>
                	<br/><span id="errorsDiv_userProfileBean.uspUserName"></span>
                </td>
              </tr>
              <tr>
                <th nowrap="nowrap"><s:text name="label.title" /><span class="star">*</span> : </th>
                <td>
                	<s:textfield id="uspTitle" name="userProfileBean.uspTitle" maxlength="50" />
                	<br/><span id="errorsDiv_userProfileBean.uspTitle"></span>
                </td>
              </tr>
              <tr>
                <th nowrap="nowrap"><s:text name="label.firstName" /><span class="star">*</span> : </th>
                <td>
                	<s:textfield id="uspFname" name="userProfileBean.uspFname" maxlength="50" />
                	<br/><span id="errorsDiv_userProfileBean.uspFname"></span>
                </td>
              </tr>
              <tr>
                <th nowrap="nowrap"><s:text name="label.middleName" /> :</th>
                <td><s:textfield name="userProfileBean.uspMname" maxlength="50" /></td>
              </tr>
              <tr>
                <th nowrap="nowrap"><s:text name="label.lastName" /><span class="star">*</span> :</th>
                <td>
                	<s:textfield id="uspLname" name="userProfileBean.uspLname" maxlength="50" />
                	<br/><span id="errorsDiv_userProfileBean.uspLname"></span>
                </td>
              </tr>
			  <!-- Start Added -->
              <tr>
                <th nowrap="nowrap">Tel no :</th>
                <td>
                	<s:textfield id="uspTelNo" name="userProfileBean.uspTelNo" maxlength="50"/>
                	<br/><span id="errorsDiv_userProfileBean.uspTelNo"></span>
                </td>
              </tr>
              <tr>
                <th nowrap="nowrap">Mobile no :</th>
                <td>
                	<s:textfield id="uspMobileNo" name="userProfileBean.uspMobileNo" maxlength="50"/>
                	<br/><span id="errorsDiv_userProfileBean.uspMobileNo"></span>
                </td>
              </tr>
              <tr>
                <th nowrap="nowrap">E-mail :</th>
                <td>
                	<s:textfield id="uspEmail" name="userProfileBean.uspEmail" maxlength="50"/>
                	<br/><span id="errorsDiv_userProfileBean.uspEmail"></span>
                </td>
              </tr>
              <s:if test="%{editMode == 'true'}">
              	<tr id="changePassBtn">
                	<th nowrap="nowrap">&nbsp;</th>
                	<td>
                		<!-- <input type="button" value="change password" onClick="onClickChangePassword();" /> -->
                		<authz:authorize granted="<%=PrivConst.S010202%>" ><input type="button" value="reset password" onClick="onClickResetPassword();" /></authz:authorize>
                		
                	</td>
              	</tr>
              	<!--  
              	<tr id="changeOldPass" style="display: none;">
                	<th nowrap="nowrap"><s:text name="label.oldPassword" /><span class="star">*</span> :</th>
                	<td>
                		<s:password id="oldPassword" name="oldPassword" maxlength="50" />
                		<br/><span id="errorsDiv_oldPassword"></span>
                	</td>
              	</tr>
              	<tr id="changeNewPass" style="display: none;">
                	<th nowrap="nowrap"><s:text name="label.newPassword" /><span class="star">*</span> :</th>
                	<td>
                		<s:password id="newPassword" name="newPassword" maxlength="50" />
                		<br/><span id="errorsDiv_newPassword"></span>
                	</td>
                </tr>
              	<tr id="changeConfirmNewPass" style="display: none;">
                	<th nowrap="nowrap"><s:text name="label.confirmNewPassword" /><span class="star">*</span> :</th>
                	<td>
                		<s:password id="confirmPassword" name="confirmPassword" maxlength="50" />
                		<br/><span id="errorsDiv_confirmPassword"></span>
                	</td>
              	</tr> -->
              </s:if>
              <s:else>
               <!--  
                <tr>
                	<th nowrap="nowrap"><s:text name="label.password" /><span class="star">*</span> :</th>
                	<td>
                		<s:password id="uspPassword" name="userProfileBean.uspPassword" maxlength="50" />
                		<br/><span id="errorsDiv_userProfileBean.uspPassword"></span>
                	</td>
                </tr>
             
              	<tr>
                	<th nowrap="nowrap"><s:text name="label.confirmPassword" /><span class="star">*</span> :</th>
                	<td>
                		<s:password id="confirmPassword" name="confirmPassword" maxlength="50" />
                		<br/><span id="errorsDiv_confirmPassword"></span>
                	</td>
              	</tr> -->
              </s:else>
              <s:if test="%{editMode == 'true'}">
              <tr>
                <th nowrap="nowrap">Expire Date :</th>
                <td>
                	<s:label id="uspExpireDate" name="userProfileBean.uspExpireDateFormatted"/>
                </td>
              </tr>
              <tr>
                <th nowrap="nowrap">Last Access Date :</th>
                <td>
                	<s:label id="uspLastAccessDate" name="userProfileBean.uspLastAccessDateFormatted"/>
                </td>
              </tr>
              </s:if>
              <tr>
                <th nowrap="nowrap"><s:text name="label.status" /><span class="star">*</span> :</th>
                <td><s:radio name="userProfileBean.uspStatus" list="statusList" listKey="value" listValue="label" /></td>
              </tr>
              <!-- End Added -->
              <tr><td colspan="2">&nbsp;</td></tr>
			  <!-- Start Added For Admin -->
			  <s:if test="%{editMode == 'true'}"> 
			  <authz:authorize granted="<%=PrivConst.S010201%>" >
			  <tr>
			  	<td><center>For Admin</center></td>
			  	<td></td>
			  </tr>
			  <tr>
                <th nowrap="nowrap">Wrong login counter :</th>
                <td>
                	<s:hidden id="uspWrongLoginCounterHidden" name="userProfileBean.uspWrongLoginCounter"/>
                	<s:label id="uspWrongLoginCounter" name="userProfileBean.uspWrongLoginCounter"/> times
                </td>
              </tr>
			  <tr>
                <th nowrap="nowrap">User lock? :</th>
                <td>
                	<s:hidden id="uspLockFlagHidden" name="userProfileBean.uspLockFlag"/>
                	<s:label id="uspLockFlag" name="userProfileBean.uspLockFlag"/>
                	<authz:authorize granted="<%=PrivConst.S010203%>" ><input type="button" value="Lock" onClick="onClickLock();" />
                	<input type="button" value="Unlock" onClick="onClickUnLock();" /></authz:authorize>
                </td>
              </tr>
              <tr>
                <th nowrap="nowrap">Force change password :</th>
                <td>
                	<s:hidden id="uspForceChngPwdFlagHidden" name="userProfileBean.uspForceChngPwdFlag"/>
                	<s:label id="uspForceChngPwdFlag" name="userProfileBean.uspForceChngPwdFlag"/>
                </td>
              </tr>
              <tr>
                <th nowrap="nowrap">Password expired :</th>
                <td>
                	<s:label id="passwordExpired" name="userProfileBean.passwordExpired"/>
                </td>
              </tr>
              </authz:authorize>
              </s:if>
			  <!-- End Added For Admin -->
			  <tr><td colspan="2">&nbsp;</td></tr>

              <tr>
                <td colspan="4" align="center" style="text-align: center">
                
                <table border="0" align="center" style="width: 630px">
                	<col /><col /><col />
                	<tr>
                		<td valign="top">
                		<span><s:text name="label.userGroup" /></span><br />
                        <select id="box1View" multiple="multiple" style="height:300px;width:300px;">
                        
                        <s:iterator value="roleList" status="rowstatus" id="roleList">
                        	<option value="<s:property value="value"/>"><s:property value="label"/></option>
                        </s:iterator>
                        
                        </select><br/>
                        <select id="box1Storage">
                    </select>
                		</td>
                
                <td align="left">
                	<table border="0">
                		<tr>
                			<td>&nbsp;</td>
                		</tr>
                		<tr>
                			<td>&nbsp;</td>
                		</tr>
                		<tr>
                			<td>&nbsp;</td>
                		</tr>
                		<tr>
                			<td><div align="center"><button id="to2" type="button">&nbsp;>&nbsp;</button></div></td>
                		</tr>
                		<tr>
                			<td><div align="center"><button id="allTo2" type="button">&nbsp;>>&nbsp;</button></div></td>
                		</tr>
                		<tr>
                			<td><div align="center"><button id="allTo1" type="button">&nbsp;<<&nbsp;</button></div></td>
                		</tr>
                		<tr>
                			<td><div align="center"><button id="to1" type="button">&nbsp;<&nbsp;</button></div></td>
                		</tr>
                	</table>     
                </td>
                
                <td align="left">
                		<span><s:text name="label.assigned" /><span class="star">*</span></span><br />
					<select id="box2View" name="assignedList" multiple="multiple" style="height:300px;width:300px;">
                    
                    <s:if test="%{editMode == 'true'}">
                    	<s:iterator value="assignedRolesList" status="rowstatus" id="role">
                        	<option value="<s:property value="value"/>"><s:property value="label"/></option>
                        </s:iterator>
                    </s:if>
                    
                    </select><br/><span id="errorsDiv_assignedList"></span>
                    <select id="box2Storage">
                    </select>
				</td>
                
                	</tr>
                </table>
				</td>
              </tr>
              
            </table>
   </div> 
 </div>
 <div class="button">
   		<table align="center">
            <tr>
              <td><authz:authorize granted="<%=PrivConst.S010201%>" ><span id="button"><a href="javascript:onClickSave();"><span><s:text name="button.save" /></span></a></span></authz:authorize></td>
              <td><span id="button"><a href="javascript:onClickBack();"><span><s:text name="button.back" /></span></a></span></td>
            </tr>
        </table>
   </div>
</s:form>
<br />
<br />
</div>

<input id="changePasswordFlag" type="hidden" />
<script>
function onClickSave(){
	var form = document.forms['userProfileManagement-add.htm'];
	var editMode = $j("#editMode").val();
	
    var rules=new Array();
    var i=0;
    rules[i++]='userProfileBean.uspTitle:Title|required';
    rules[i++]='userProfileBean.uspFname:First name|required';
    rules[i++]='userProfileBean.uspLname:Last name|required';
    rules[i++] = 'validateAssignedRole()|custom';
    rules[i++] = 'userProfileBean.uspEmail:Email|regexp|^([\\w\\.-]+?@[\\w-\\.]+)*$';
	if(editMode != "true"){
		// Add
		/**** comment by pimonrath because must be reset password *****/
		
		//rules[4]='userProfileBean.uspPassword:Password|required';
		rules[i++]='userProfileBean.uspUserName:Username|required';
		//rules[6]='confirmPassword|required';
		
		//if($j("#uspPassword").val() != "" && $j("#confirmPassword").val() != "")
			//rules[7] = 'confirmPassword:Confirm Password|equal|$userProfileBean.uspPassword:Password';
	}else{
		// Edit
		/**** comment by pimonrath because must be reset password *****/
		
		/* if($j("#changePasswordFlag").val() == "true"){
			rules[4]='newPassword:Password|required';
			rules[5] = 'oldPassword:Old Password|required';
			rules[6]='confirmPassword|required';
			
			if($j("#newPassword").val() != "" && $j("#confirmPassword").val() != "")
				rules[7] = 'confirmPassword:Confirm Password|equal|$newPassword:Password';
			
		} */
		
	}
	
	

	
    if (yav.performCheck('mainForm', rules, 'inline')){
    	selectAllOptions();
    	
		if(editMode == "true"){
			$j("#mainForm").attr("action","userProfileManagement-edit.htm"); 
		}
			
    	
		$j("#mainForm").submit();
  		
	}else{
	}
	
}

function onClickBack(){
    $j("#mainForm").attr("action","userProfileManagement-search.htm"); 
  	$j("#mainForm").submit();
}

function onClickChangePassword(){
	$j("#changePasswordFlag").val("true");
	
	$j("#changePassBtn").hide();
	$j("#changeOldPass").show();
	$j("#changeNewPass").show();
	$j("#changeConfirmNewPass").show();
	
}

function validateAssignedRole() {
	if (document.getElementById("box2View").options.length == 0) {
		return MESSAGE_CODE_USP0003 + "__inline__assignedList";
	}
}

function selectAllOptions(){
	var ref = document.getElementById("box2View");

	for(i=0; i<ref.options.length; i++)
	   ref.options[i].selected = true;

}
function onClickLock(){
	$j("#mainForm").attr("action","userProfileManagement-userLock.htm");
	$j("#uspLockFlagHidden").val('<%=ProjectConstant.Y_FLAG%>');
	$j("#mainForm").submit();
}
function onClickUnLock(){
	$j("#mainForm").attr("action","userProfileManagement-userLock.htm");
	$j("#uspLockFlagHidden").val('<%=ProjectConstant.N_FLAG%>');
	$j("#mainForm").submit();
}

function onClickResetPassword(){
	
	//var editMode = $j("#editMode").val();

    //var rules=new Array();
    //rules[0]='userProfileBean.uspTitle:Title|required';
    //rules[1]='userProfileBean.uspFname:First name|required';
    //rules[2]='userProfileBean.uspLname:Last name|required';
    
	//rules[3] = 'validateAssignedRole()|custom';
	//if(editMode != "true"){
	//	rules[4]='userProfileBean.uspUserName:Username|required';
	//}
	
    //if (yav.performCheck('mainForm', rules, 'inline')){
    //	selectAllOptions();
	//}

    $j("#mainForm").attr("action","userProfileManagement-resetPassword.htm");
	$j("#mainForm").submit();
}
</script>
    